package com.honey.mybatis.generator;

import java.util.ArrayList;
import java.util.List;

import org.java.plugin.Plugin;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.InnerClass;
import com.honey.compilation.java.InnerEnum;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.StaticBlock;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.ContentType;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.generator.java.extension.point.GeneratorJavaClass;
import com.honey.mybatis.generator.model.StateEnum;

/**
 * 生成Model类
 * @author Administrator
 *
 */
public class MybatisModelExtension extends AbstractMybatisExtension  implements  GeneratorJavaClass{

	private IntrospectSchemaColumn introspectSchemaColumn;
	
	private IntrospectSchema introspectSchema;
	
	private AbstractProduceJavaCompilation abstractProduceJavaCompilation ;
	
	private AbstractJavaCompilation abstractJavaCompilation = null;
	
	public MybatisModelExtension(Plugin belongPlugin) {
		super(belongPlugin);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#createNewCompilation(com.honey.core.dbmapping.introspect.IntrospectSchema, com.honey.core.dbmapping.introspect.IntrospectSchemaColumn)
	 */
	@Override
	public AbstractJavaCompilation createNewCompilation( IntrospectSchema introspectSchema, IntrospectSchemaColumn introspectSchemaColumn) {
		this.introspectSchema = introspectSchema;
		this.introspectSchemaColumn = introspectSchemaColumn;
		abstractProduceJavaCompilation = MybatisFactory.getAbstractProduceJavaCompilation(MybatisFactory.MybatisClass.PRODUCE_MODEL, mybatisPlugin, introspectSchema, introspectSchemaColumn);
		try {
			abstractJavaCompilation = abstractProduceJavaCompilation.produceCompilation();
		} catch (CompilationException e) {
			
			e.printStackTrace();
		}
		return abstractJavaCompilation;
	}
	
	@Override
	public FullyQualifiedJavaType getType() {
		return abstractJavaCompilation.getType();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#importImportClass()
	 */
	@Override
	public FullyQualifiedJavaType[] importImportClass() {
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#importInnerClass()
	 */
	@Override
	public InnerClass[] importInnerClass() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#importInnerEnumClass()
	 */
	@Override
	public InnerEnum[] importInnerEnumClass() {
		InnerEnum[]  answer = null;
		List< InnerEnum > list = new ArrayList<InnerEnum>();
 		AbstractProduceJavaCompilation inner = MybatisFactory.getAbstractProduceJavaCompilation(MybatisFactory.MybatisClass.PRODUCE_MODEL_TABLE_CLOUMN, mybatisPlugin, introspectSchema, introspectSchemaColumn); 
		try {
			for(Column column  : introspectSchemaColumn.allColumns()){
				if( column.getEnumSchema() != null){
					list.add(
							(InnerEnum) new StateEnum(mybatisPlugin, column.getEnumSchema()).
							createAbstractJavaCompilation(introspectSchema, introspectSchemaColumn)
					);
				}
			}
			InnerEnum innerenum = (InnerEnum) inner.produceCompilation();
			innerenum.addMethod( inner.produceMethods() );
			innerenum.addField(inner.produceFields());
			list.add(innerenum);
			
			answer = new InnerEnum[list.size() ];
			list.toArray(answer);
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return answer;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#importMethods()
	 */
	@Override
	public Method[] importMethods() {
		Method[]  answer = null ;
		try {
			answer = abstractProduceJavaCompilation.produceMethods();
		} catch (CompilationException e) {
			e.printStackTrace();
		}		
		return answer ;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#importStaticBlocks()
	 */
	@Override
	public StaticBlock[] importStaticBlocks() {
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMybatisExtension#clean()
	 */
	@Override
	public void clean() {
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#importFields()
	 */
	@Override
	public Field[] importFields() {
		return null;
	}

	/* (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#getContentType()
	 */
	@Override
	public ContentType getContentType() {
		
		return ContentType.JAVA_MODEL;
	}
	
	
}
